import Footer from "../../components/footer.vue"
import SearchTop from "../../components/newSearchTop.vue";
import bindPhone from "../../components/bindPhone.vue"
import SearchTitle from "../../components/newSearchTitle.vue";
import axios from 'axios';
import $store from "../../components/vuex.js" 
export default {
    components: {
        Footer: Footer,
        SearchTop,
        bindPhone,
        SearchTitle,
    },
    data() {
        return {
            giftId: this.$route.query.giftId,
            lenovoId: window.sessionStorage.getItem("lenovoId"),
            token: window.sessionStorage.getItem("token"),
            data: { bag: {} },
            guseLike: [],
            hotGiftList: [],
            gameurl: "http://pitf.vgs.lenovo.com.cn",
            getGift:false,
        }
    },
    filters: { // 自定义时间过滤器

        giftType: function(value) { // 过滤礼包类型
            if (value == '0') {
                return "免费礼包";

            } else if (value == '1') {
                return "积分礼包";

            } else if (value = '=2') {

                return "付费礼包";

            } else {
                return "礼包";
            }
        },
    },
    mounted() {
    	
        console.log(this.$route.query.giftId)
        this.getGiftDetails()
        this.getHotGift()

    },
    methods: {
        getGiftDetails() { // 获取礼包详情
            let url = `/gift/api/giftbag/queryGiftBagDetails.do?code=${this.giftId}&lenovoId=${this.lenovoId}`;
            let that = this;
            axios.get(url).then(function(res) {

                that.data = res.data
                that.guseLike = res.data.youLike.slice(0, 3)
                if(res.data.bag.whether=="0"){ // 未领取
                    that.getGift = false

                }else if(res.data.bag.whether=="1"){// 已领取
                     that.getGift = true

                }else{

                }

            }, function(err) {

            })

        },
        getHotGift() { // 获取热门礼包
            let url = "/gift/api/giftbag/queryHotGiftBag.do"
            let that = this;
            axios.get(url).then(function(res) {
                if (res.data.code == 0 || JSON.stringify(res.data.giftBags) != '[]') {
                    that.hotGiftList = res.data.giftBags.slice(0, 3)
                } else {
                    $toast.center("请求错误")
                }
            }, function(err) {

            })
        },

        goGiftDeta(giftId) { // 领取当前页面的其他礼包
            var url = window.location.href
            console.log(window.location)
            console.log(this.$route.path)
            url = url.split("?")[0] + "?giftId=" + giftId

            window.location.href = url
            window.location.reload()

            this.$router.push({ path: "../newGiftDetails", query: { giftId: giftId } })

        },
        getGift(giftId) { // 领取礼包
        	let lenovoId = window.sessionStorage.getItem("lenovoId");
            let url = `/gift/api/giftbag/getGiftBag.do?code=${giftId}&lenovoId=${lenovoId}`
            let that = this;
            let _url = "/back/bss/lid/lidIsBind?lenovoId=" + lenovoId;

            axios.get(_url).then(function(res) {

                if (res.data.code == 0) { // 已经绑定过手机号,该Id就是真实的lenovoId
                    $store.commit("allShowThree")
                    $store.commit("bindPhone", false)
                    axios.get(url).then(function(res) {

		                that.$toast.top(res.data.msg)

		            }, function(err) {

		            })

                } else if (res.data.code == 228) { // 未绑定手机号 无效的联想账号

                    $store.commit("allShowTwo");
                    $store.commit("bindPhone", true)
                    $store.commit('show')
                } else {

                }
            })



          




        },
        //开始游戏
        gameStart(cpId, gameName, softId, serverId, serviceName) { //  cpId 游戏名称  softId ,区服id,区服名
            let that = this;
            var url = `${this.gameurl}/gameRedirect/loginGame?cp=${cpId}_GAME&game=${softId}&gameName=${gameName}&token=${this.token}`

            this.saveGame(window.sessionStorage.getItem("lenovoId"), cpId, softId, 48, serverId, serviceName, gameName)
            window.open(url)
            reportEvent.pageStartGame(this.userId)
        },
        saveGame(lenovoId, cpId, softId, relativeId, serverId, serviceName, gameName) { // 记录玩过的游戏 联想ID cpId softId  平台编号 区服id 区服名 游戏名
            let that = this;
            if (serverId && serviceName) {
                this.resSaveGame(lenovoId, cpId, softId, relativeId, softId, serverId, gameName, serviceName)
            } else {
                console.log(softId)
                let _url = this.gmConf.domainHttps + "unionlogin.4366.com/lenovo/gamecenter/gslist.do?jsonp&serverType=GENERAL&order=opentime:dasc&gsPageSize=5&online" + "&game=" + softId + "&gsPageNo=1"
                console.log(_url)
                that.jqajax(_url, { type: "post", dataType: "jsonp" }, function(res) {

                    var _servers = res[softId].servers;
                    var arr = []
                    for (var item in _servers) {
                        arr.push(_servers[item]);
                    }

                    that.newService = arr[0]
                    console.log(that.newService)

                    that.resSaveGame(lenovoId, cpId, softId, relativeId, that.newService.id, that.newService.name, gameName)

                });
            }

        },
        resSaveGame(lenovoId, cpId, softId, relativeId, serverId, serviceName, gameName) {
            let that = this
            let url = `/gift/api/webpagegame/collectionGame.do?lenovoId=${lenovoId}&relativeId=${relativeId}&serverId=${serverId}&softId=${softId}&serverName=${serviceName}&gameName=${gameName}&softName=${cpId}`;
            axios.get(url).then(function(res) {
                if (res.data.code == 0) {

                } else {
                    that.$toast.center("请求错误")
                }

            }, function(err) {

            })

        },
    }
}